package com.security.utils;

import cn.hutool.json.JSONUtil;
import com.security.domain.bo.R;
import jakarta.servlet.http.HttpServletResponse;

import java.io.IOException;
import java.io.PrintWriter;

/**
 * 响应工具类
 *
 * @author DblSun
 */
@SuppressWarnings("all")
public class ResponseUtil {

    public static void sendResponse(HttpServletResponse response, R r) throws IOException {
        response.setContentType("application/json");
        response.setCharacterEncoding("UTF-8");
        response.setStatus(r.getCode());
        try (PrintWriter writer = response.getWriter()) {
            writer.write(JSONUtil.toJsonStr(r, JsonConfigUtil.config));
            writer.flush();
        } finally {
            response.getWriter().close();
        }
    }
}
